<template>
  <div class="wrapper">
    <div class="title">我的订单</div>
    <div class="orders">
      <div class="order"
      v-for="(item,index) in list" :key="index">
        <div class="order_title">
          {{item.shopName}}
          <span class="order_status">{{item.isCanceled?'已取消':'已下单'}}</span>
        </div>
        <div class="order_content">
          <div class="order_content_imgs">
            <template
              v-for="(innerItem,innerIndex) in item.products"
              :key="innerIndex">
              <img
                class="order_content_img"
                :src="innerItem.product.img"
                v-if="innerIndex <= 3"
                alt="">
            </template>
          </div>
          <div class="order_info">
            <div class="order_info_price">￥{{item.totalPrice}}</div>
            <div class="order_info_count">共{{item.count}}件</div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <Docker :currentIndex="2"></Docker>
</template>
<script>
import { reactive, toRefs } from 'vue'
import { get } from '@/utils/request'
import Docker from '@/components/Docker.vue'
// 处理订单逻辑
const useOrderListEffect = () => {
  const data = reactive({ list: [] })
  const getOrderList = async () => {
    const result = await get('/api/order')
    if (result?.errno === 0 && result?.data?.length) {
      data.list = result.data
      const tempData = result.data
      tempData.forEach(item => {
        let totalPrice = 0
        let count = 0
        const products = item.products || []
        products.forEach(it => {
          count += (it?.orderSales || 0)
          totalPrice += ((it?.product?.price * it?.orderSales) || 0)
        })
        item.totalPrice = totalPrice
        item.count = count
      })
    }
  }
  getOrderList()
  const { list } = toRefs(data)
  return { list }
}
export default {
  name: 'OrderList',
  data () {
    return {
    }
  },
  components: {
    Docker
  },
  setup () {
    const { list } = useOrderListEffect()
    return { list }
  }
}
</script>
<style lang="scss" scoped>
.wrapper{
  overflow-y: auto;
  position: absolute;
  left: 0;
  top: 0;
  bottom: .5rem;
  right: 0;
  background: rgb(248, 248, 248);
  // padding: 0 .18rem .1rem .18rem;
}
.title{
  line-height: .44rem;
  background: #fff;
  font-size: .16rem;
  color: #333;
  text-align: center;
}
.order{
  margin: .16rem .18rem;
  padding: .16rem;
  background: #fff;
  &_title{
    line-height: .22rem;
    font-size: .16rem;
    color: #333;
  }
  &_status{
    float: right;
    font-size: .14rem;
    color: #999;
  }
  &_content{
    display: flex;
    &_imgs{
      flex: 1;
    }
    &_img{
      width: .4rem;
      height: .4rem;
      margin-right: .12rem;
    }
  }
  &_info{
    width: .7rem;
    &_price{
      margin-bottom: .04rem;
      line-height: .2rem;
      font-size: .14rem;
      text-align: right;
      color: #e93b3b;
    }
    &_count{
      line-height: .14rem;
      font-size: .12rem;
      text-align: right;
      color: #333;
    }
  }
}
</style>
